/*
 * Copyright 2007 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sateh.spaces.simple;

import java.io.IOException;
import java.rmi.MarshalledObject;
import java.util.*;

public class SimpleSpaceObject
{
    private final MarshalledObject marshalledObject;

    private String className;

    private final Map<String,Object> properties;

    private long timeToLive;

    private long creationTime = System.currentTimeMillis();

    private final UUID id = UUID.randomUUID();

    private final Collection<String> interfaces = new HashSet<String>();

    private final Collection<String> classes = new HashSet<String>();
    
    public MarshalledObject getMarshalledObject()
    {
        return marshalledObject;
    }

    public String getClassName()
    {
        return className;
    }

    public long getTimeToLive()
    {
        return timeToLive;
    }

    public long getCreationTime()
    {
        return creationTime;
    }

    public boolean isExpired()
    {
        return (timeToLive != Long.MAX_VALUE) && (System.currentTimeMillis() > (creationTime + timeToLive));
    }

    public UUID getId()
    {
        return id;
    }

    public Collection<String> getInterfaces()
    {
        return interfaces;
    }

    public Collection<String> getClasses()
    {
        return classes;
    }

    //

    public void renew(long timeToLive)
    {
        this.creationTime = System.currentTimeMillis();
        this.timeToLive = timeToLive;
    }

    //

    public SimpleSpaceObject(Object object, long timeToLive) throws IOException
    {
        this.className = object.getClass().getName();
        this.marshalledObject = new MarshalledObject(object);
        this.properties = SimpleSpaceUtils.getObjectProperties(object);
        this.timeToLive = timeToLive;
        
        // Collect classes and interfaces

        SimpleSpaceUtils.collectClassNames(classes, object.getClass());
        SimpleSpaceUtils.collectInterfaceNames(interfaces, object.getClass().getInterfaces());
    }

    /**
     *
     */

    public Set<String> keys()
    {
        return properties.keySet();
    }

    /**
     *
     */

    public void put(String name, Object value)
    {
        properties.put(name, value);
    }

    /**
     *
     */

    public Object get(String name)
    {
        return properties.get(name);
    }

    /**
     *
     */

    public boolean contains(String name)
    {
        return properties.containsKey(name);
    }
}
